import BaseView from "../baseView.js";


export default class AddStudentView extends BaseView {
    render() {
        $("#showContent").html(
            `<h1>添加学生</h1>
            <hr/>
            <form class="layui-form" action="#" lay-filter="stuForm" id="stuFrom">
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="stuName" id="username" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input" style="width:100px">
                </div>
            </div>
                <div class="layui-form-item">
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-block">
                    <input type="text" name="stuAge" id="age" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input" style="width:100px">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="stuGender" value="男" title="男" checked="">
                    <input type="radio" name="stuGender" value="女" title="女">
                </div>
            </div>

            <div class="layui-form-item">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-block">
                <input type="text" name="stuName" id="tel" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input" style="width:100px">
            </div>
            </div>
    
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" id="addBtn" class="layui-btn" lay-submit="" lay-filter="newStuBtn">新增</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
            `
        )
    }

    hander() {
        layui.use(['element', 'form', 'table', 'layer'], function () {

            let { element, form, table, $, layer } = layui;
            form.render();

            $("#addBtn").click(() => {
                $.ajax({
                    type: "post",
                    url: "/student/addstudent/",
                    data: {
                        name: $("#username").val(), age: $("#age").val(), gender: $('input[type="radio"]:checked').val(), tel: $("#tel").val()
                    },
                    dataType: "json",
                    success: (data) => {
                        if (data.code == 0) {
                            layer.msg(data.msg)
                            window.location.href = "#/main/studentlist"
                            // alert(data.msg)
                        } else {
                            // alert(data.msg)
                            layer.msg(data.msg)
                        }

                    }

                })
            })

        });



    }

}